package com.tengju.bff.interfaces.manage.crm.dashborad.facade;

import com.tengju.bff.interfaces.manage.crm.dashborad.assembler.DashboardMarketSortAssembler;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.DashboardBrandMarketSortVO;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.DashboardSECGoodsMarketSortVO;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.clickhouse.dashboard.DashBoardMarketInfo;
import com.tengju.data.domain.model.clickhouse.dashboard.DashBoardMarketRespository;
import com.tengju.data.domain.model.clickhouse.dashboard.MonthQuarterEnum;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @Date: 2021/4/29 11:11
 * @Description:
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DashboardMarketSortFacade {

    private final DashBoardMarketRespository dashBoardMarketRespository;


    /**
     * 工作台KA获取品牌销售榜
     * @param dateType 日期类型  1：当前月   2:上月  3:当前季度
     * @date 2021/5/7 14:57
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO getKABrandMarketSortInfo(Integer dateType) {
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        List<Long> idCodeList = new ArrayList<>();
        List<DashBoardMarketInfo> infoList = new ArrayList<>();
        genSecIdCodes.forEach(e -> {
            idCodeList.add(e.getValue());
        });
        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(dateType);
        if (monthQuarterEnum == null) {
            throw new RuntimeException("找不到对应的日期类型");
        }

        LocalDate localDate = LocalDate.now();

        if (CollectionUtils.isNotEmpty(idCodeList)) {
            infoList = dashBoardMarketRespository.getKABrandMarketSort(dateType, DateUtil.getStartMonth(monthQuarterEnum, localDate),
                    DateUtil.getEndMonth(monthQuarterEnum, localDate), DateUtil.getMediumMonth(monthQuarterEnum, localDate), idCodeList);
        }
        List<DashboardBrandMarketSortVO> voList = DashboardMarketSortAssembler.assemblerMarketSortVOList(infoList,localDate,monthQuarterEnum);

        TableVO table = new TableVO();
        List<ColumnVO> columns = List.of(
                ColumnVO.of("brank","排名",false),
                ColumnVO.of("brandName","品牌",false),
                ColumnVO.of("sellAccount","销售额",true),
                ColumnVO.of("hodeRatio","占比",false)
        );
        List<Map<String, Object>> rows = new ArrayList<>();
        for(DashboardBrandMarketSortVO marketSortVO : voList) {
            Map<String, Object> row = new HashMap<>();
            row.put("brank",marketSortVO.getBrank());
            row.put("brandName",marketSortVO.getBrandName());
            row.put("sellAccount",marketSortVO.getSellAccount());
            row.put("hodeRatio",marketSortVO.getHodeRatio());
            row.put("brandId",marketSortVO.getBrandId());

            rows.add(row);

        }

        table.setHeader(columns);
        table.setRecords(rows);
        return table;
    }

    /**
     * 工作台SEC获取品牌销售榜
     * @param secIdCode
     * @param dateType
     * @date 2021/5/7 19:39
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO getSECBrandMarketSortInfo(Long secIdCode,Integer dateType) {
        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(dateType);
        if (monthQuarterEnum == null) {
            throw new RuntimeException("找不到对应的日期类型");
        }
        LocalDate localDate = LocalDate.now();

        List<Long> secIdCodeList = new ArrayList<>();
        secIdCodeList.add(secIdCode);

        List<DashBoardMarketInfo> infoList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(secIdCodeList)) {
            infoList = dashBoardMarketRespository.getSECBrandMarketSort(dateType,DateUtil.getStartMonth(monthQuarterEnum,localDate),
                    DateUtil.getEndMonth(monthQuarterEnum,localDate),DateUtil.getMediumMonth(monthQuarterEnum,localDate),secIdCodeList);
        }
        List<DashboardBrandMarketSortVO> voList = DashboardMarketSortAssembler.assemblerMarketSortVOList(infoList,localDate,monthQuarterEnum);

        TableVO table = new TableVO();
        List<ColumnVO> columns = List.of(
                ColumnVO.of("brank","排名",false),
                ColumnVO.of("brandName","品牌",false),
                ColumnVO.of("sellAccount","销售额",true),
                ColumnVO.of("hodeRatio","占比",false)
        );
        List<Map<String, Object>> rows = new ArrayList<>();
        for(DashboardBrandMarketSortVO marketSortVO : voList) {
            Map<String, Object> row = new HashMap<>();
            row.put("brank",marketSortVO.getBrank());
            row.put("brandName",marketSortVO.getBrandName());
            row.put("sellAccount",marketSortVO.getSellAccount());
            row.put("hodeRatio",marketSortVO.getHodeRatio());
            row.put("brandId",marketSortVO.getBrandId());

            rows.add(row);

        }

        table.setHeader(columns);
        table.setRecords(rows);
        return table;

    }


    /**
     * 工作台SEC获取商品销售榜
     * @param secIdCode  前端传过来的secIdCode
     * @param dateType  日期类型
     * @date 2021/5/7 16:28
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO getSECGoodsmarketSortinfo(Long secIdCode,Integer dateType) {
        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(dateType);
        if (monthQuarterEnum == null) {
            throw new RuntimeException("找不到对应的日期类型");
        }
        LocalDate localDate = LocalDate.now();

        List<Long> secIdCodeList = new ArrayList<>();
        secIdCodeList.add(secIdCode);

        List<DashBoardMarketInfo> infoList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(secIdCodeList)) {
            infoList = dashBoardMarketRespository.getSecGoodsMarketSort(dateType,DateUtil.getStartMonth(monthQuarterEnum,localDate),
                    DateUtil.getEndMonth(monthQuarterEnum,localDate),DateUtil.getMediumMonth(monthQuarterEnum,localDate),secIdCodeList);
        }
        List<DashboardSECGoodsMarketSortVO> voList = DashboardMarketSortAssembler.assemblerSECMarketSortVOList(infoList,localDate,monthQuarterEnum);

        TableVO table = new TableVO();
        List<ColumnVO> columns = List.of(
                ColumnVO.of("brank","排名",false),
                ColumnVO.of("goodsName","商品",false),
                ColumnVO.of("sellAccount","销售额",true)
        );
        List<Map<String, Object>> rows = new ArrayList<>();

        for (DashboardSECGoodsMarketSortVO secGoodsMarketSortVO : voList) {
            Map<String,Object> row = new HashMap<>();
            row.put("brank",secGoodsMarketSortVO.getBrank());
            row.put("goodsName",secGoodsMarketSortVO.getGoodsName());
            row.put("sellAccount",secGoodsMarketSortVO.getSellAccount());
            row.put("spuId",secGoodsMarketSortVO.getSpuId());

            rows.add(row);
        }

        table.setHeader(columns);
        table.setRecords(rows);
        return table;


    }

}
